﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using BurningSphere.Simulator.Properties;

namespace BurningSphere.Simulator.Controls
{
	/// <summary>
	/// Simple control for testing the laser orientation
	/// </summary>
	public class TestControl : LaserControl
	{
		string type = "DoNothing";
		Matrix laserMatrix;
		float x=0;
		float y=0;

		/// <summary>
		/// Ctor
		/// </summary>
		/// <param name="type"></param>
		public TestControl()
		{
		}

		/// <summary>
		/// Final matrix
		/// </summary>
		public override Matrix LaserMatrix
		{
			get
			{
				return laserMatrix;	
			}
		}

		/// <summary>
		/// Type of test
		/// </summary>
		public string Type
		{
			get
			{
				return type;
			}
			set
			{
				type = value;
			}
		}

		/// <summary>
		/// Update method - simple case statement for
		/// redirecting to the relevant test method
		/// </summary>
		/// <param name="matrix"></param>
		public override void Update(Matrix matrix)
		{
			switch (type)
			{
				case "DoNothing":
					DoNothing(matrix);
					break;
				case "LineRotated45X":
					UpdateLineRotated45X(matrix);
					break;
				case "LineRotated45Y":
					UpdateLineRotated45Y(matrix);
					break;
				case "RandomY":
					RandomY(matrix);
					break;

			}
		}

		/// <summary>
		/// Random heart monitor style effect
		/// </summary>
		/// <param name="matrix"></param>
		private void RandomY(Matrix matrix)
		{
			Random rnd = new Random(DateTime.Now.Millisecond * DateTime.Now.Second * DateTime.Now.Hour);
			int rndInt = (rnd.Next() % 500) - 250;

			y = (float)Math.Sin(Geometry.DegreeToRadian(((float)rndInt)) / 50.0f );
			TargetXY(matrix);
		}

		/// <summary>
		/// Keep the laser still (apart from the movement of
		/// the core laser matrix)
		/// </summary>
		/// <param name="matrix"></param>
		private void DoNothing(Matrix matrix)
		{
			laserMatrix = matrix;
		}

		/// <summary>
		/// Rotate the laser 45 degrees round the X axis and
		/// scroll round Y
		/// </summary>
		/// <param name="matrix"></param>
		private void UpdateLineRotated45X(Matrix matrix)
		{
			y = (float)Math.Sin(Geometry.DegreeToRadian(45f));
			TargetXY(matrix);
		}

		/// <summary>
		/// Rotate the laser 45 degrees round the Y axis and
		/// scroll round the X
		/// </summary>
		/// <param name="matrix"></param>
		private void UpdateLineRotated45Y(Matrix matrix)
		{
			y += 0.01f; if (y > 1.0f) y -= 2.0f;
			x = (float)Math.Sin(Geometry.DegreeToRadian(45f));
			TargetXY(matrix);
		}

		/// <summary>
		/// Never end
		/// </summary>
		/// <returns></returns>
		public override bool MoveNext()
		{
			return true;
		}

		/// <summary>
		/// Utility function for converting the X and Y values to a matrix
		/// </summary>
		/// <param name="matrix"></param>
		private void TargetXY(Matrix matrix)
		{
			Matrix rotateY = Matrix.RotationY(Geometry.DegreeToRadian(x) * 180.0f);
			Matrix rotateX = Matrix.RotationX(Geometry.DegreeToRadian(y) * 180.0f);
			laserMatrix = rotateX * rotateY * matrix;
		}
	}
}
